package demo.proxy;

import org.springframework.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

public class Main {

    public static void main(String[] args) {
//        //静态代理
//        HouseProxy proxy = new HouseProxy(new RealHouseSubject());
//        proxy.rentHouse();

//        //动态代理
//        //目标对象
//        RealHouseSubject target = new RealHouseSubject();
//        //动态生成代理对象
//        HouseSubject proxy = (HouseSubject) Proxy.newProxyInstance(target.getClass().getClassLoader(),
//                new Class[]{HouseSubject.class},
//                new JDKInvocationHandler(target));
//        proxy.rentHouse();
//        proxy.saleHouse();

        //CGLib代理
        //目标对象
        RealHouseSubject target = new RealHouseSubject();
        HouseSubject houseSubject = (HouseSubject) Enhancer.create(target.getClass(), new CGLibMethodInterceptor(target));
        houseSubject.rentHouse();
        houseSubject.saleHouse();
    }
}
